package com.lly.bigdata.bgayx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lly.bigdata.bgayx.common.utils.StringUtils;
import com.lly.bigdata.bgayx.entity.SysRole;
import com.lly.bigdata.bgayx.entity.SysRoleMenu;
import com.lly.bigdata.bgayx.mapper.SysRoleMapper;
import com.lly.bigdata.bgayx.mapper.SysRoleMenuMapper;
import com.lly.bigdata.bgayx.mapper.SysUserRoleMapper;
import com.lly.bigdata.bgayx.service.ISysRoleService;
import com.lly.bigdata.bgayx.vo.Result;
import com.lly.bigdata.bgayx.vo.SearchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author lly
 * @since 2020-10-21
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    SysRoleMapper sysRoleMapper;
    @Autowired
    SysRoleMenuMapper sysRoleMenuMapper;
    @Autowired
    SysUserRoleMapper sysUserRoleMapper;

    /**
     * 查询分页数据
     *
     * @param searchModel
     * @return
     */
    @Override
    public Result queryPage(SearchModel searchModel) {
        IPage<SysRole> page = new Page(searchModel.getPageNumber(), searchModel.getPageSize());
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper();
        if (StringUtils.isNotEmpty(searchModel.getS_name())) {
            queryWrapper.like("role_name", searchModel.getS_name());
        }
        IPage<SysRole> pagas = sysRoleMapper.selectPage(page, queryWrapper);
        Result res = Result.pageOk(page.getRecords());
        res.setTotal(page.getTotal());
        res.setTotal(pagas.getPages());
        return res;
    }

    @Override
    public  List<SysRole>  queryRole(SearchModel searchModel) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper();
        if (StringUtils.isNotEmpty(searchModel.getS_name())) {
            queryWrapper.like("role_name", searchModel.getS_name());
        }
        List<SysRole> sysRoles = sysRoleMapper.selectList(queryWrapper);
        return sysRoles;
    }

    @Override
    public boolean ckRoleName(Long id, String rolename) {
        if (id != null) {
            SysRole role = baseMapper.selectById(id);
            if (role == null) {
                return true;
            }
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_name", rolename);

        List<SysRole> list = baseMapper.selectList(queryWrapper);
        if (list.size() > 0) {
            return false;
        }
        return true;
    }

    @Override
    @Transactional(readOnly = false)
    public Result addRole(SysRole role, String menus, String userid) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_name", role.getRoleName());
        SysRole roles = baseMapper.selectOne(queryWrapper);
        if (roles == null) {
            role.setCreateTime(LocalDateTime.now());
            role.setCreateUserId(userid);
            baseMapper.insert(role);
            List<String> ss = StringUtils.splitList(menus);
            for (String s : ss) {
                insertRoleMenu(role.getRoleId(), s);
            }
        } else {
            return Result.fail("角色名称已存在！");
        }
        return Result.ok();
    }

    private void insertRoleMenu(long roleid, String s) {
        SysRoleMenu roleMenu = new SysRoleMenu();
        roleMenu.setId(UUID.randomUUID().toString().replace("-", ""));
        roleMenu.setRoleId(roleid);
        roleMenu.setMenuId(s);
        sysRoleMenuMapper.insert(roleMenu);
    }

    @Override
    public Result updateRole(SysRole role, String menus) {
        SysRole role2 = baseMapper.selectById(role.getRoleId());
        if (role2 == null) {
            return Result.fail("您要修改数据不存在！");
        } else {
            boolean istrue = false;
            if (role.getRoleName().equals(role2.getRoleName())) {
                istrue = true;
            } else {
                if (ckRoleName(null, role.getRoleName())) {
                    istrue = true;
                } else {
                    return Result.fail("您要修改的角色名称已存在！");
                }
            }
            if (istrue) {
                baseMapper.updateById(role);
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq("role_id", role.getRoleId());
                sysRoleMenuMapper.delete(queryWrapper);
                List<String> ss = StringUtils.splitList(menus);
                if (ss.size() > 0) {
                    for (String s : ss) {
                        insertRoleMenu(role.getRoleId(), s);
                    }
                }
            }
        }
        return Result.ok();
    }

    @Override
    @Transactional(readOnly = false)
    public Result delRole(Long id) {
        //删除用户角色 删除角色和模块
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_id", id);
        sysRoleMenuMapper.delete(queryWrapper);
        //  JkfwUserRoleExample example1 = new JkfwUserRoleExample();
        //example1.createCriteria().andRoleIdEqualTo(id);
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("role_id", id);
        sysUserRoleMapper.delete(queryWrapper1);
        baseMapper.deleteById(id);
        return Result.ok();
    }

}
